import { reqRegionTreeAPI } from "@/apis/common"
import type { ChatTarget } from "@/types/chatTarget"
import type { RegionTree } from "@/types/common"
import type { Notify } from "@/types/notify"
import { defineStore } from "pinia"
import { ref } from "vue"

/*
 +----------------------------------------------------------------------
 + Title        : 公共 pinia 存储
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2024-12-24
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 公共 pinia 存储
 +----------------------------------------------------------------------
*/
export const useCommonStore = defineStore('common', () => {

    // 当前路由
    const currentRouteUrl = ref<string>('')

    // 聊天对象列表
    const chatTargetList = ref<ChatTarget[]>([])

    // 未读通知数量
    const unreadNotifyTotal = ref<number>(0)

    // 好友申请数量
    const friendApplyTotal = ref<number>(0)

    // 地区树
    const regionTrees = ref<RegionTree[]>([])

    // 最新通知
    const newestNotify = ref<Notify>()

    /**
     * 设置聊天对象列表
     * 
     * @param ChatTarget[] item ------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const setChatTargetList = (items: ChatTarget[]) => {
        chatTargetList.value = items
    }

    /**
     * 清空聊天对象列表
     * 
     * @returns void
     * @author huwl
     */
    const clearChatTargetList = () => {
        chatTargetList.value = []
    }

    /**
     * 添加
     * 
     * @param ChatTarget item --------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const addChatTarget = (item: ChatTarget) => {
        chatTargetList.value.unshift(item)
    }

    /**
     * 修改
     * 
     * @param ChatTarget item --------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const editChatTarget = (item: ChatTarget) => {
        const updateChatTarget:any = chatTargetList.value.find((value: ChatTarget) => value.id == item.id)
        for (const key in item) {
            updateChatTarget[key as never] = item[key as never]
        }
    }

    /**
     * 设置未读消息数量
     * 
     * @param number total -----------------------------------------数量
     * @returns void
     * @author huwl
     */
    const setUnreadNotifyTotal = (total: number) => {
        unreadNotifyTotal.value = total
    }

    /**
     * 清空未读消息数量
     * 
     * @returns void
     * @author huwl
     */
    const clearUnreadNotifyTotal = () => {
        unreadNotifyTotal.value = 0
    }

    /**
     * 设置好友申请数量
     * 
     * @param number total -----------------------------------------数量
     * @returns void
     * @author huwl
     */
    const setFriendApplyTotal = (total: number) => {
        friendApplyTotal.value = total
    }

    /**
     * 清空好友申请数量
     * 
     * @returns void
     * @author huwl
     */
    const clearFriendApplyTotal = () => {
        friendApplyTotal.value = 0
    }

    /**
     * 获取地区树
     * 
     * @param any data ---------------------------------------------参数
     * @returns RegionTree[]
     * @author huwl
     */
    const getRegionTree = async (data?: any) => {
        if (regionTrees.value.length == 0) {
            const result = await reqRegionTreeAPI(data)
            regionTrees.value = result.data
        }

        return regionTrees.value
    }

    /**
     * 清空地区
     * 
     * @returns void
     * @author huwl
     */
    const clearRegion = () => {
        regionTrees.value = []
    }

    /**
     * 设置最新通知
     * 
     * @param Notify notify ----------------------------------------通知
     * @returns void
     * @author huwl
     */
    const setNewestNotify = (notify: Notify) => {
        newestNotify.value = notify
    }

    /**
     * 清空最新通知
     * 
     * @returns void
     * @author huwl
     */
    const clearNewestNotify = () => {
        newestNotify.value = {} as never
    }

    return {
        currentRouteUrl,
        chatTargetList,
        friendApplyTotal,
        unreadNotifyTotal,
        newestNotify,
        regionTrees,
        


        setChatTargetList,
        clearChatTargetList,
        addChatTarget,
        editChatTarget,
        setFriendApplyTotal,
        clearFriendApplyTotal,
        setUnreadNotifyTotal,
        clearUnreadNotifyTotal,
        setNewestNotify,
        clearNewestNotify,
        getRegionTree,
        clearRegion
    }
},
// 持久化
{
    persist: {
        storage: {
            getItem(key) {
                return uni.getStorageSync(key)
            },
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
        }
    }
})